package com.kdtech.analyse.video;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;

/**
 * 漳州新闻网 解析类
 *
 * @author Chase
 *
 */
public class ZznewsVideoAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		if(url.indexOf("photo") != -1 || url.indexOf("tb2011") != -1) {
			return bRet;
		}
		String[] regex = {
				"http://www.zznews.cn/shipin/.*/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9].*.shtml"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		String url = urlMeta.getUrl();
		/**
		 * 判断是否为详细页，不是详细页面直接返回空
		 */
		if (!isDetailPage(url)) {
			System.err.println("不符合规则");
		}

		String html = urlMeta.getHtml();

		/*
		 * 定义新闻实体需要的字段
		 */
		String title = null;
		String desc = null;
		Long date = null;
		String commentNum = null;
		String clickNum = null;

		Document doc = Jsoup.parse(html);

		date = DateUtils.matchDate(doc.select("div.n-cont-top").text());
		if (date == null) {
			date = DateUtils.matchDate(url);
		}

		title = doc.select("h1").text();// 标题
		desc = doc.select("div#news-content p").text();// 标题
		NewsMeta meta = new NewsMeta();
		meta.setTitle(title);
		meta.setContent(desc);
		meta.setUrl(url);
		meta.setType(2);
		meta.setClickNum(NumberUtils.parseInt(clickNum));
		meta.setCommentNum(NumberUtils.parseInt(commentNum));
		meta.setDate(date);

		/**
		 * 解析用于更新的地址
		 */
		String updateUrl= null;
		meta.setUpdateUrl(updateUrl);

		String author= JSoupUtils.matchAuthor(doc,  "稿源:","来源:");
		meta.setAuthor(author);
		return meta;
	}
	
	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}


	
}
